package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/axios")
@CrossOrigin
public class AxiosController {
    @Autowired
    private UserService userService;
    @GetMapping("getUserById")
    public User getUserById(Integer id){
        return userService.findUserById(id);
    }
    @GetMapping("user/{sex}/{age}")
    public List<User> user(User user){
        return userService.findUserBySexAndAge(user);
    }
    @GetMapping("getUser")
    public List<User> getUser(User user){
        return userService.findUserByNameAndSexAndAge(user);
    }
    @DeleteMapping("/deleteById/{id}")
    public String delete(@PathVariable Integer id){
        Integer i = userService.deleteById(id);
        return "影响了"+i+"条";
    }
    @PostMapping("/saveUser")
    public String save(@RequestBody User user){
        Integer i = userService.saveUser(user);
        return "修改了"+i+"条";
    }
    @PutMapping("update")
    public String updateById(@RequestBody User user){
        Integer i = userService.updateById(user);
        return "修改了"+i+"条";
    }
    @PutMapping("updateByName/{whereName}")
    /**
     * restful可以为对象的属性赋值
     * 注意restful的名称不要与属性重名,否则会覆盖,引发bug
     */
    public String updateByName(@PathVariable String whereName,@RequestBody User user){
        Integer i = userService.updateByName(whereName,user);
        return "修改了"+i+"条";
    }
}
